package com.dongdongshop.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.aliyun.auth.credentials.provider.StaticCredentialProvider;
import com.aliyun.sdk.service.dysmsapi20170525.AsyncClient;
import com.aliyun.sdk.service.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.sdk.service.dysmsapi20170525.models.SendSmsResponse;
import com.dongdongshop.constant.AliyunConstant;
import com.dongdongshop.service.SmsService;
import com.dongdongshop.util.Result;
import com.google.gson.Gson;
import darabonba.core.client.ClientOverrideConfiguration;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import wiremock.net.minidev.json.JSONObject;

import javax.annotation.Resource;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;

@Service
@DubboService
public class SmsServiceImpl implements SmsService {

    @Resource
    private StaticCredentialProvider provider;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public void sendPhone(String phone) {
        // Configure the Client
        AsyncClient client;
        client = AsyncClient.builder()
                .region("cn-hangzhou") // Region ID
                //.httpClient(httpClient) // Use the configured HttpClient, otherwise use the default HttpClient (Apache HttpClient)
                .credentialsProvider(provider)
                //.serviceConfiguration(Configuration.create()) // Service-level configuration
                // Client-level configuration rewrite, can set Endpoint, Http request parameters, etc.
                .overrideConfiguration(
                        ClientOverrideConfiguration.create()
                                .setEndpointOverride("dysmsapi.aliyuncs.com")
                        //.setConnectTimeout(Duration.ofSeconds(30))
                )
                .build();
        // Parameter settings for API request
        JSONObject json = new JSONObject();
        String number = RandomUtil.randomNumbers(4);
        json.put(AliyunConstant.SMS_CODE,number);
        //TimeUnit 表示给定单元粒度的时间段。TimeUnit是java.util.concurrent下面的一个枚举类，它提供在给定单元中进行跨单元转换和执行计时及延迟操作的实用工具方法。MINUTES分钟
        stringRedisTemplate.boundValueOps(phone).set(number);
        //设置过期时间为30分钟   枚举类TimeUnit.MINUTES这个方法就是指分钟
        stringRedisTemplate.boundValueOps(phone).expire(30,TimeUnit.MINUTES);
        SendSmsRequest sendSmsRequest = SendSmsRequest.builder()
                .phoneNumbers(phone)
                .signName(AliyunConstant.SIGN)
                .templateCode(AliyunConstant.TEMPLATECODE)
                .templateParam(json.toJSONString())
                .build();
        // Asynchronously get the return value of the API request
        try {
            CompletableFuture<SendSmsResponse> response = client.sendSms(sendSmsRequest);
            // Synchronously get the return value of the API request
            SendSmsResponse resp = response.get();
            System.out.println(new Gson().toJson(resp));
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            client.close();
        }
    }
}
